/**
 * 维修收入结算维护 - 编辑页面
 * @author sxc
 * @date 2019/01/18
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            MMRW005: null,
            form: {
                MMRWPLANT_NAME: null,
                MMRWLEGAL_NAME: null,
                MMRW016_NAME: null,
                MMRW017_NAME: null,
                MMRWUSER: null,
                MMRWGRUP: null,
                MMRWORIG: null,
                MMRWORIU: null
            },
            rules: {
                GWCSDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCSDOCDATE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCS010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCS117: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWCS118: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            }
        },
        cols = [[
            { field: 'MMRWSEQ', type: 'checkbox', title: '选择'}
            , {field: 'MMRW001', width: 160,title: '配件编号'}
            , {field: 'GWAML003',width: 160,title: '品名'}
            , {field: 'GWAML004',width: 160,title: '规格'}
            , {field: 'MMRW002',width: 160,title: '单位'}
            , {field: 'MMRW003', width: 80,title: '数量', edit: 'text'}
            , {field: 'MMRW005', width: 80,title: '仓库'}
            , {field: 'MMRW015', width: 80,title: '备注', edit: 'text'}
            , {field: 'MMRW011', width: 80,hide: true}
            , {field: 'MMRW012', width: 80,hide: true}
        ]],
        callObj,
        moduleName,
        listData = null,
        table, gwei024Table, layOpenIndex
    ;

    var Gwei024Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwei024_edit',
                    _loadTplsFull: [{
                        dom: 'common_open_multi_baogong', tpl: 'common/_open_multi_baogong' //0
                    },{
                        dom: 'common_open_window', tpl: 'common/_open_window' //1
                    }
                    ]
                }, win);
            },
            
            initTable: function () {
                layui.use(['table'], function () {
                    table = layui.table;
                    gwei024Table = table.render({
                        elem: app.getByid(doc, 'gwei024_edit_table'),
                        cellMinWidth: 80,
                        id: 'gwei024_edit_table',
                        defaultToolbar:false,
                        cols: cols,
                        data: listData,
                        page: false
                    });

                    table.on('tool(gwei024_edit_table)', function (obj) {

                    });
                    //监听单元格编辑
                    table.on('edit(gwei024_edit_table)', function (obj) {
                        if (obj.field == 'MMRW003') {//数量
                            var parnt = /^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])$/;
                            if(!parnt.test(obj.value)){
                                app.showWarning("数量必须大于0！");
                                return;
                            }
                        }
                    });
                });
            },
            tableReload: function (listData) {
                self._reloadTable({id: 'gwei024_edit_table', data: listData});
            },

            addData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwei024/addData',
                    xdata: listData,
                    toJsonStr: true,
                    okmsg: moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwei024').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            editData: function (listData) {
                app.ajax({
                    xurl: 'crm/gwei024/editData',
                    xdata: listData,
                    toJsonStr: true,
                    okmsg: moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwei024').reloadTableCurr();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            _call: function (tagData) {
                moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if (tagData.is_check) {
                    vueData.isCheck = true;
                } else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    app.ajax({
                        xurl: 'crm/gwei024/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: result.data.data,
                                    reset: true
                                });
                                var listData = [];
                                self.tableReload(listData);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    vueData.isCheck = tagData.is_check;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwei024/getMmrwData',
                        xdata: {MMRWDOCNO: callObj.data.MMRWDOCNO},
                        cb: function (result) {
                            if(result.ok){
                                if(result.data.data.length > 0){
                                    self.tableReload(result.data.data);
                                    /*app.ajax({
                                        xurl: 'crm/gwei024/getMB002',
                                        xdata: {MMRWPLANT: result.data.data[0].MMRWPLANT},
                                        cb: function (result) {
                                            if(result.ok){
                                                if(result.data.data != null){
                                                    vueData.form.MMRWPLANT_NAME = result.data.data.MB002;
                                                }
                                            }
                                        }
                                    });*/
                                }
                            }
                        }
                    });
                }
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['MMRW_T'],vueData.form);
                var vue = new Vue({
                    el: '#gwei024_edit',
                    data: vueData,
                    watch: {

                    },
                    methods: {
                        addData: function () {
                            if (vueData.form.MMRW016 == '' || vueData.form.MMRW016 == null) {
                                app.showWarning("请先选择人员！");
                                return;
                            }
                            if (vueData.form.MMRW017 == '' || vueData.form.MMRW017 == null) {
                                app.showWarning("请先选择部门！");
                                return;
                            }
                            var choose_data = gwei024Table.getDatas();
                            /*var count = 0;
                            if(choose_data.length > 0) {
                                count = choose_data[choose_data.length - 1].MMRWSEQ;
                            }
                            choose_data.push({
                                MMRWSEQ: count + 1,
                                MMRW001: '',
                                GWAML003: '',
                                GWAML004: '',
                                MMRW002: '',
                                MMRW003: '',
                                MMRW005: '',
                                MMRW015: '',
                                MMRW011: '', //报工单号
                                MMRW012: '' //报工单项次
                            });
                            self.tableReload(choose_data);*/
                            var str_data = [];
                            choose_data.forEach(function (v) {
                                var str = v.MMRW011 + v.MMRW012;
                                str_data.push(str);
                            });
                            var count = 0;
                            if(choose_data.length > 0) {
                                count = choose_data[choose_data.length - 1].MMRWSEQ;
                            }
                            self._openTplFull({
                                tag: 'common_open_multi_baogong',
                                tagData: {
                                    operation: 'add',
                                    STR: str_data.join(','),
                                    MMRW017: vueData.form.MMRW017,
                                    cb: function (data) {
                                        for(var i = 0; i< data.length; i++){
                                            choose_data.push({
                                                MMRWSEQ: count + 1 + i,
                                                MMRW001: data[i]["MMRH003"],
                                                GWAML003: data[i]["GWAML003"],
                                                GWAML004: data[i]["GWAML004"],
                                                MMRW002: data[i]["GWAM008"],
                                                MMRW003: data[i]["MMRH004"],
                                                MMRW005: vueData.MMRW005,
                                                MMRW015: data[i]["MMRG013"],
                                                MMRW011: data[i]["MMRHDOCNO"], //报工单号
                                                MMRW012: data[i]["MMRHSEQ"] //报工单项次
                                            });
                                        }
                                        self.tableReload(choose_data);
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择报工单信息',
                                    area: ['1000px', '530px']
                                }
                            });
                        },
                        cancle: function () {
                            layer.close(layOpenIndex);
                        },
                        delData: function () {
                            listData = gwei024Table.getDatas();
                            var checkStatus = table.checkStatus('gwei024_edit_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    listData.removeByMap({
                                        MMRWSEQ: v.MMRWSEQ
                                    });
                                });
                                self.tableReload(listData);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var flag = true;
                                    var listData = gwei024Table.getDatas();
                                    if(listData.length < 1){
                                        app.showWarning('表格数据不能为空！');
                                        return;
                                    }
                                    for (var i = 0; i < listData.length; i++) {
                                        if(listData[i].MMRW003 <= 0){
                                            app.showWarning('数量必须大于0！');
                                            flag = false;
                                            return;
                                        }
                                    }
                                    listData.addMap(vueData.form, false);
                                    if(flag){
                                        if (vueData.isAdd) {
                                            self.addData(listData);
                                        } else {
                                            self.editData(listData);
                                        }
                                    }

                                }
                            });
                        },
                        openMmrw017Window:function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_id',
                                        columnList: [{label: '部门编号', prop: 'ID', width: '150'},
                                            {label: '部门名称', prop: 'NAME', width: '150'}]
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRW017_NAME = data.NAME;
                                        vueData.form.MMRW017 = data.ID;
                                        app.ajax({
                                            xurl: 'crm/gwei024/getGwas001',
                                            xdata:{GWAS002:data.ID},
                                            cb: function (result) {
                                                if(result.ok){
                                                    if(result.data.data.length > 0){
                                                        vueData.MMRW005 = result.data.data[0].GWAS001;
                                                    }

                                                }
                                            }
                                        });
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择部门信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openMmrwlegalWindow:function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'cq_sysorg_biz_2',
                                        columnList: [{label: '归属法人编号', prop: 'ID', width: '150'},
                                            {label: '归属法人名称', prop: 'NAME', width: '150'},
                                            {label: '营运中心编号', prop: 'BIZ_CODE', width: '150'},
                                            // {label: '营运中心名称', prop: 'BIZ_CODE_NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRWLEGAL_NAME = data.NAME;
                                        vueData.form.MMRWLEGAL = data.ID;
                                        vueData.form.MMRWPLANT = data.BIZ_CODE;
                                        //vueData.form.MMRWPLANT_NAME = data.BIZ_CODE_NAME;
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择归属法人信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        clearMmrwlegal:function() {
                            vueData.form.MMRWLEGAL_NAME = '';
                            vueData.form.MMRWLEGAL = '';
                            vueData.form.MMRWPLANT = '';
                        },
                        openMmrw016Window:function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [{label: '人员编号', prop: 'ID', width: '150'},
                                            {label: '人员名称', prop: 'NAME', width: '150'}]
                                    },
                                    cb: function (data) {
                                        vueData.form.MMRW016_NAME = data.NAME;
                                        vueData.form.MMRW016 = data.ID;
                                    }
                                },
                                index: 1,
                                openOpts: {
                                    title: '选择人员信息',
                                    area: ['600px', '470px']
                                }
                            });
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwei024_edit');
                    }
                });
                self.initTable();
            },
            _lazy: function () {
                this._super();
            }
        });

    return new Gwei024Edit();

});